//
//  SureOrderViewController.m
//  HuiMWorld
//
//  Created by chirui on 2018/1/16.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "SureOrderViewController.h"
#import "OrderTableViewCell.h"
#import "AddressListViewController.h"
#import "ShopPayForViewController.h"
#import "UserTicketsViewController.h"


@interface SureOrderViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
    UITableView * _tableView;
    NSArray * _dataArray;
    
    UILabel * name;
    UILabel * phone;
    UILabel * addressLabel;
    NSString * addressStr;  // 地址编号
    
    NSDictionary * addressDic;
}



@end

@implementation SureOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    TitleView(@"提交订单");
    
//     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"img_nav_bg"] forBarMetrics:UIBarMetricsDefault];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatTableView];
    
    [self creatEndView];

        addressDic = self.dataDic[@"data_address"];
         _dataArray  = self.dataDic[@"lists"];
  
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_return_back" target:self action:@selector(backBtnClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatEndView{
    
    
    UIView * endView =[[UIView alloc]init];
    
    if (ISPX) {
        endView.frame = CGRectMake(0, HEIGHT-64-48-34, WIDTH, 48+32);
    }else{
        endView.frame = CGRectMake(0, HEIGHT-64-48, WIDTH, 48);
    }
    endView.backgroundColor = NavBackColor;
    
    UIButton * endBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    endBtn.frame = CGRectMake(0, 0, WIDTH, 48);
    endBtn.backgroundColor = [UIColor orangeColor];
    [endBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [endBtn setTitle:@"确认订单" forState:UIControlStateNormal];
    [endBtn addTarget:self action:@selector(sureBuy:) forControlEvents:UIControlEventTouchUpInside];
    
    [endView addSubview:endBtn];
    [self.view addSubview:endView];
    
}

-(void)sureBuy:(UIButton *)btn{
    
    if (addressStr.length>0||addressStr !=nil) {
        
        if ([self.typeStr isEqualToString:@"goods"]) {
             [self creatOrderDataRequest];
        }else {

            [self creatCarOrderDataRequest];
        }

    }else {

        SHuiMBhudTextTimeStr(@"请选择地址！");

        return;
    }
}


-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64 -48)
                                            style:UITableViewStyleGrouped];
    
    if (ISPX) {
        
        _tableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT-64-48-34);
    }
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor =[UIColor whiteColor];
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[OrderTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[OrderTableViewCell reuseIdentifier]];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
    

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    
    OrderTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[OrderTableViewCell reuseIdentifier] forIndexPath:indexPath];
    [cell.goodsImage sd_setImageWithURL:ImageUrl(dic[@"cover_photo"]) placeholderImage:ImageName(@"")];
    cell.goodsName.text = dic[@"product_name"];
    cell.goodsName.frame = CGRectMake(98, 10, WIDTH -110, 20);
    cell.goodsName.numberOfLines = 2;
    [cell.goodsName sizeToFit];
    cell.priceLabel.text = [NSString stringWithFormat:@"￥%@",dic[@"price"]];
    cell.numberLabel.text = [NSString stringWithFormat:@"X%@",dic[@"quantity"]];
  
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView * heardView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 100)];
    heardView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH,90)];
    view.backgroundColor = [UIColor whiteColor];
    
    [view tapAction:^{
        AddressListViewController * address = [AddressListViewController new];
        address.exChangeGoods = @"choose";
        address.block = ^(NSDictionary * dic){
            addressDic = dic;
            [tableView reloadData];
        };
        [self.navigationController pushViewController:address animated:YES];
     
    }];
    UIImageView * bgImage =[[UIImageView alloc]initWithFrame:CGRectMake(5, 35, 20, 20)];
    bgImage.userInteractionEnabled = YES;
    bgImage.image = ImageName(@"icon_add_submission");
    [view addSubview:bgImage];
    
    name =[[UILabel alloc]initWithFrame:CGRectMake(25, 15, 120, 20)];
    name.font =[UIFont systemFontOfSize:15];
    [view addSubview:name];
    
    phone =[[UILabel alloc]initWithFrame:CGRectMake(145, 15, WIDTH-150, 20)];
    phone.font =[UIFont systemFontOfSize:15];
    [view addSubview:phone];
    
    addressLabel =[[UILabel alloc]initWithFrame:CGRectMake(25, 35, WIDTH-70, 20)];
    addressLabel.font =[UIFont systemFontOfSize:15];
    addressLabel.numberOfLines=2;
    addressLabel.text = @" 请选择地址";
    [addressLabel sizeToFit];
    [view addSubview:addressLabel];
    

        if ([addressDic isKindOfClass:[NSDictionary class]]) {
            name.text =[NSString stringWithFormat:@"%@",addressDic[@"fullname"]];
            phone.text = addressDic[@"phone"];
            addressLabel.frame = CGRectMake(25, 50, WIDTH-70, 20);
            addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@",addressDic[@"province"],addressDic[@"city"],addressDic[@"county"],addressDic[@"address"]];
            addressLabel.numberOfLines=2;
            [addressLabel sizeToFit];
            addressStr = addressDic[@"id"];

    }else {
       
    }
    UIImageView * imageView =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 30, 20, 30, 30)];
    imageView.image =[UIImage imageNamed:@"icon_information_more"];
    [view addSubview:imageView];
    
    [heardView addSubview:view];
    return heardView;
}

-(void)creatOrderDataRequest {
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",address_url,creat_order_api];


    NSDictionary *  dic = @{@"pro_id":self.dataDic[@"lists"][0][@"pro_id"],
                            @"address_id":addressStr,
                            @"quantity":[NSString stringWithFormat:@"%@",self.dataDic[@"lists"][0][@"quantity"]],
                            @"mid":USER_DEFAULTGet(@"mid"),
                            @"option_id":self.dataDic[@"lists"][0][@"option_id"],
                            @"token":USER_DEFAULTGet(@"token"),
                            };

    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:dic  Success:^(id responseObject) {

        if ([responseObject[@"code"] intValue]==200) {

            ShopPayForViewController * payFor = [[ShopPayForViewController alloc]init];
            payFor.dataDic = responseObject[@"datas"];
            [self.navigationController pushViewController:payFor animated:YES];

        }else {

            SHuiMBhudTextTimeStr(responseObject[@"mark"]);

        }

    } failed:^(NSError *error) {

        SHuiMBhudTextNetError;
    }];
}


-(void)creatCarOrderDataRequest {

    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",address_url,creatCart_order_api];


    
    NSDictionary *  dic = @{@"cart_ids":self.cart_idStr,
                            @"address_id":addressStr,
                            @"mid":USER_DEFAULTGet(@"mid"),
                            @"token":USER_DEFAULTGet(@"token"),
                            };

    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:dic  Success:^(id responseObject) {

        if ([responseObject[@"code"] intValue]==200) {

            ShopPayForViewController * payFor = [[ShopPayForViewController alloc]init];
            payFor.dataDic = responseObject[@"datas"];
            [self.navigationController pushViewController:payFor animated:YES];

        }else {

            SHuiMBhudTextTimeStr(responseObject[@"mark"]);

        }

    } failed:^(NSError *error) {

        SHuiMBhudTextNetError;
    }];
}


@end
